<template>
<!-- 热门品牌 -->
<div ref="target">
  <HomePanel title="热门品牌" sub-title="国际经典 品质保证">
    <template v-slot:right>
      <a href="javascript:;" class="iconfont icon-angle-left prev" @click="activeIndex=0" :class="{disabled:activeIndex===0}"></a>
      <a href="javascript:;" class="iconfont icon-angle-right next" @click="activeIndex=1" :class="{disabled:activeIndex===1}"></a>
    </template>
    <div class="box" ref="box">
    <transition name="fade">
      <ul v-if="result.length" class="list" :style="`transform: translateX(${ activeIndex ? '-50%' : 0 });`">
        <li v-for="i in result" :key="i.id">
          <RouterLink to="/">
            <img v-lazy="i.picture" alt="">
          </RouterLink>
        </li>
      </ul>
      <div v-else class="skeleton">
         <XtxSkeleton class="item" v-for="i in 5" :key="i" width="240px" height="305px"  bg="#e4e4e4"></XtxSkeleton>
        </div>
      </transition>
    </div>
  </HomePanel>
  </div>
</template>

<script>
import { findBrand } from '@/api/home'
import HomePanel from './home-panel'
import { ref } from 'vue'
import { useLazyData } from '@/hooks/index'
export default {
  name: 'HomeBrand',
  components: { HomePanel },
  setup () {
    // const brands = ref([])
    // findBrand(10).then(data => {
    //   brands.value = data.result
    // })
    // console.log(brands)
    // 可以通过() => findBrand(10)拿到10条数据
    const { result, target } = useLazyData(() => findBrand(10))
    const activeIndex = ref(0)
    console.log(result)
    return { result, activeIndex, target }
  }
}
</script>

<style scoped lang='less'>
.home-panel {
  background:#f5f5f5
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: @xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;
    li {
      margin-right: 10px;
      width: 240px;
      &:nth-child(5n) {
        margin-right: 0;
      }
      img {
        width: 240px;
        height: 305px;
      }
    }
  }
  .skeleton {
    width: 100%;
    display: flex;
    .item {
      margin-right:10px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
